package com.wh.concurrent.Interrupt;
/*sleep（）方法的实现检查到休眠线程被中断，它会相当友好地终止线程，并抛出InterruptedException异常。另外一种情况，如果线程在调用sleep（）方法前被中断，
那么该中断称为待决中断，它会在刚调用sleep（）方法时，立即抛出InterruptedException异常*/
public class SleepInterrupt extends Object implements Runnable{

	@Override
	public void run() {
		try {
			System.out.println("in run() - about to sleep for 20 seconds");
			Thread.sleep(20000);
			System.out.println("in run() - woke up");  
		} catch (InterruptedException e) {
			System.out.println("in run() - interrupted while sleeping");
			return;
		}
		System.out.println("in run end normally");
	}
	
	public static void main(String[] args) {
		Thread thread = new Thread(new SleepInterrupt());
		thread.start();
		//主线程休眠2秒，从而确保刚才启动的线程有机会执行一段时间  
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    System.out.println("in main() - interrupting other thread");  
        //中断线程t  
	    thread.interrupt();  
        System.out.println("in main() - leaving");  
	}

}
